﻿using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;

[AddComponentMenu ("Event/RZ 2D Raycaster")]
[RequireComponent (typeof (Camera))]
public class RZ2DRaycaster : Physics2DRaycaster
{
    [System.NonSerialized] private List<RaycastResult> m_raycasts = new List<RaycastResult> (64);

    [System.NonSerialized] private List<IRZRaycast> m_rzRays = new List<IRZRaycast> (64);

    int count;

    public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
    {
        base.Raycast (eventData, m_raycasts);// 射线触碰到的物件数


        var len = m_raycasts.Count;
        for (int i = 0; i < len; i++)
        {
            m_raycasts[i].gameObject.GetComponents (m_rzRays);
            count = m_rzRays.Count;

            if (count == 0)
            {
                resultAppendList.Add (m_raycasts[i]); // 添加
                continue;
            }

            for (int j = 0; j < count; j++)
            {
                if (m_rzRays[j].IsRaycastValid ())
                {
                    resultAppendList.Add (m_raycasts[i]);
                    break;
                }
            }
            m_rzRays.Clear ();
        }
        m_raycasts.Clear ();
    }
}